﻿
Imports System.Data.SqlClient

Public Class DLocalidad

    Inherits Conexion
    Dim cmd As New SqlCommand


    Public Function guardar(ByVal dts As Entidades.Elocalidad) As Boolean
        Try
            conectado()
            cmd = New SqlCommand("sp_altalocalidad")
            cmd.CommandType = CommandType.StoredProcedure
            cmd.Connection = cnn
            cmd.Parameters.AddWithValue("@cod_localidad", dts.codloc)
            cmd.Parameters.AddWithValue("@nombre_localidad", dts.nombreloc)
            cmd.Parameters.AddWithValue("@codigo_postal", dts.codigopostal)
            cmd.Parameters.AddWithValue("@cod_departamento", dts.dpto)

            If cmd.ExecuteNonQuery Then
                Return True
            Else
                Return False
            End If
        Catch ex As Exception
            MsgBox(ex.Message)
            Return False
        Finally
            desconectado()
        End Try
    End Function


    Public Function modificar(ByVal localidad As Entidades.Elocalidad) As Boolean
        Try
            conectado()
            cmd = New SqlCommand("sp_modificarlocalidad")
            cmd.CommandType = CommandType.StoredProcedure
            cmd.Connection = cnn
            cmd.Parameters.AddWithValue("@cod_localidad", localidad.codloc)
            cmd.Parameters.AddWithValue("@nombre_localidad", localidad.nombreloc)
            cmd.Parameters.AddWithValue("@codigo_postal", localidad.codigopostal)
            cmd.Parameters.AddWithValue("@cod_departamento", localidad.dpto)

            If cmd.ExecuteNonQuery Then
                Return True
            Else
                Return False
            End If
        Catch ex As Exception
            MsgBox(ex.Message)
            Return False
        Finally
            desconectado()
        End Try
    End Function


    Public Function buscar(ByVal LOCALIDAD As String) As DataTable
        Try
            conectado()
            cmd = New SqlCommand("sp_buscarlocalidad")
            cmd.CommandType = CommandType.StoredProcedure
            cmd.Connection = cnn
            cmd.Parameters.AddWithValue("@nombre_localidad", LOCALIDAD)
            Dim DA As New SqlDataAdapter(cmd)
            Dim TABLA As New DataTable
            TABLA.Clear()
            DA.Fill(TABLA)
            Return TABLA

            Return Nothing

        Catch ex As Exception
            MsgBox(ex.Message)
            Return Nothing
        Finally
            desconectado()
        End Try
    End Function

    Public Function buscardepartamento(ByVal DEPARTAMENTO As String) As DataTable
        Try
            conectado()
            cmd = New SqlCommand("sp_buscarcodigodep")
            cmd.CommandType = CommandType.StoredProcedure
            cmd.Connection = cnn
            cmd.Parameters.AddWithValue("@nombre_departamento", DEPARTAMENTO)
            Dim DA As New SqlDataAdapter(cmd)
            Dim TABLA As New DataTable
            TABLA.Clear()
            DA.Fill(TABLA)
            Return TABLA
            Return Nothing
        Catch ex As Exception
            MsgBox(ex.Message)
            Return Nothing
        Finally
            desconectado()
        End Try
    End Function
End Class
